package com.kgc.publicprovider.controller;

import com.kgc.publiccommon.remote.UserRemote;
import com.kgc.publiccommon.util.ResponseMsg;
import com.kgc.publicprovider.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
public class UserController implements UserRemote {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;


    @Override
    public ResponseMsg login(@RequestParam("name") String name, @RequestParam("password") String password, @RequestParam("checkCode") String checkCode) {
        logger.info("provider UserController login name:{},password:{},checkCode:{}", name, password, checkCode);
        ResponseMsg msg = userService.login(name, password, checkCode);
        logger.debug("provider UserController login msg:{}", msg);
        return msg;
    }

    @Override
    public String getCaptcha() {
        logger.info("provider getCaptcha is start...");
        String s = userService.getCaptcha();
        logger.debug("provider getCaptcha s:{}", s);
        return s;
    }

    @Override
    public ResponseMsg logout(@RequestBody Map<String, String> request) {
        logger.info("provider logout is start...");
        String sessionId = request.get("sessionId");
        if (sessionId == null || sessionId.isEmpty()) {
            ResponseMsg msg = new ResponseMsg();
            msg.setCode("400");
            msg.setMsg("缺少sessionId参数");
            return msg;
        }
        ResponseMsg msg = userService.logout(sessionId);
        logger.debug("provider logout msg:{}", msg);
        return msg;
    }

    @Override
    public ResponseMsg selectRoleById(int id) {
        logger.info("UserController selectRoleById id:{}",id);
        ResponseMsg msg = userService.selectRoleById(id);
        logger.debug("UserController selectRoleById msg:{}",msg);
        return msg;
    }

    @Override
    public ResponseMsg selectAllUser() {
        logger.info("UserController selectAllUser is start...");
        ResponseMsg msg = userService.selectAllUser();
        logger.debug("UserController selectAllUser msg:{}",msg);
        return msg;
    }

    @Override
    public ResponseMsg selectUserByMap(Map<String, Object> map) {
        logger.info("UserController selectUserByMap is start...");
        return userService.selectUserByMap(map);
    }
}
